package com.frank.oj.controller.admin;

import com.frank.model.entity.problem.Tag;
import com.frank.model.entity.problem.TagClassification;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.service.admin.problem.AdminTagService;
import javafx.scene.control.Tab;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author frank
 * @Date 2024/4/2
 */
@RestController
@RequestMapping("/api/admin/tag")
@RequiresAuthentication
@RequiresRoles(value = {"root", "problem_admin"}, logical = Logical.OR)
public class AdminTagController {

    @Resource
    private AdminTagService adminTagService;

    /**
     * 添加标签
     * @param tag 标签信息
     * @return tag
     */
    @PostMapping()
    public CommonResult<Tag> addTag(@RequestBody Tag tag){
        try {
            return CommonResult.successResponse(adminTagService.addTag(tag));
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    /**
     * 删除标签
     * @param tid 标签id
     * @return null
     */
    @DeleteMapping()
    public CommonResult<Void> deleteTag(@RequestParam("tid") Long tid){
        try {
            adminTagService.deleteTag(tid);
            return CommonResult.successResponse();
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    /**
     * 修改标签
     * @param tag 标签信息
     * @return null
     */
    @PutMapping()
    public CommonResult<Void> updateTag(@RequestBody Tag tag){
        try {
            adminTagService.updateTag(tag);
            return CommonResult.successResponse();
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @GetMapping("/classification")
    public CommonResult<List<TagClassification>> getTagClassification(@RequestParam(value = "oj",defaultValue = "ME")String oj){
        return CommonResult.successResponse(adminTagService.getTagClassification(oj));
    }
    @PostMapping("/classification")
    public CommonResult<TagClassification> addTagClassification(@RequestBody TagClassification tagClassification) {
        try {
            return CommonResult.successResponse(adminTagService.addTagClassification(tagClassification));
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @PutMapping("/classification")
    public CommonResult<Void> updateTagClassification(@RequestBody TagClassification tagClassification) {
        try {
            adminTagService.updateTagClassification(tagClassification);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @DeleteMapping("/classification")
    public CommonResult<Void> deleteTagClassification(@RequestParam("tcid") Long tcid) {
        try {
            adminTagService.deleteTagClassification(tcid);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }


}
